using Reflector.CodeModel;

namespace LanguageWriterHelper
{
    public class StatementVisitor : VisitorBase
    {
        public void Visit(IStatement value)
        {
            Visit(value as IAttachEventStatement);
            Visit(value as IBlockStatement);
            Visit(value as IBreakStatement);
            Visit(value as ICommentStatement);
            Visit(value as IConditionStatement);
            Visit(value as IContinueStatement);
            Visit(value as IDebugBreakStatement);
            Visit(value as IDoStatement);
            Visit(value as IExpressionStatement);
            Visit(value as IFixedStatement);
            Visit(value as IForEachStatement);
            Visit(value as IForStatement);
            Visit(value as IGotoStatement);
            Visit(value as ILabeledStatement);
            Visit(value as ILockStatement);
            Visit(value as IMemoryCopyStatement);
            Visit(value as IMemoryInitializeStatement);
            Visit(value as IMethodReturnStatement);
            Visit(value as IRemoveEventStatement);
            Visit(value as ISwitchStatement);
            Visit(value as IThrowExceptionStatement);
            Visit(value as ITryCatchFinallyStatement);
            Visit(value as IUsingStatement);
            Visit(value as IWhileStatement);
        }

        public void Visit(IAttachEventStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IAttachEventStatement");

            WriteStartElement("Event");
            Manager.ExpressionVisitor.Visit(value.Event);
            WriteEndElement();

            WriteStartElement("Listener");
            Manager.ExpressionVisitor.Visit(value.Listener);
            WriteEndElement();

            WriteEndElement();
        }

        public void Visit(IBlockStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IBlockStatement");

            WriteStartElement("Statements");
            Visit(value.Statements);
            WriteEndElement();
            
            WriteEndElement();
        }
        
        public void Visit(IStatementCollection value)
        {
            if (value == null || value.Count == 0)
                return;

            WriteStartElement("IStatementCollection");
            
            foreach (IStatement stat in value)
            {
                Visit(stat);
            }
            
            WriteEndElement();
        }
        
        public void Visit(IBreakStatement value)
        {
            if (value == null)
                return;

            WriteEmptyElement("IBreakStatement");
        }

        public void Visit(ICommentStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("ICommentStatement");

            WriteStartElement("Comment");
            Manager.ILVisitor.Visit(value.Comment);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IConditionStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IConditionStatement");

            WriteStartElement("Condition");
            Manager.ExpressionVisitor.Visit(value.Condition);
            WriteEndElement();

            WriteStartElement("Else");
            Visit(value.Else);
            WriteEndElement();

            WriteStartElement("Then");
            Visit(value.Then);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IContinueStatement value)
        {
            if (value == null)
                return;

            WriteEmptyElement("IContinueStatement");
        }
        public void Visit(IDebugBreakStatement value)
        {
            if (value == null)
                return;

            WriteEmptyElement("IDebugBreakStatement");
        }
        public void Visit(IDoStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IDoStatement");

            WriteStartElement("Body");
            Visit(value.Body);
            WriteEndElement();

            WriteStartElement("Condition");
            Manager.ExpressionVisitor.Visit(value.Condition);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IExpressionStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IExpressionStatement");

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();
            
            WriteEndElement();
        }

        public void Visit(IFixedStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IFixedStatement");

            WriteStartElement("Body");
            Visit(value.Body);
            WriteEndElement();

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();

            WriteStartElement("Variable");
            Manager.ILVisitor.Visit(value.Variable);
            WriteEndElement();
            
            WriteEndElement();
        }

        public void Visit(IForEachStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IForEachStatement");

            WriteStartElement("Body");
            Visit(value.Body);
            WriteEndElement();

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();

            WriteStartElement("Variable");
            Manager.ILVisitor.Visit(value.Variable);
            WriteEndElement();
            
            WriteEndElement();
        }

        public void Visit(IForStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IForStatement");

            WriteStartElement("Body");
            Visit(value.Body);
            WriteEndElement();

            WriteStartElement("Condition");
            Manager.ExpressionVisitor.Visit(value.Condition);
            WriteEndElement();

            WriteStartElement("Increment");
            Visit(value.Increment);
            WriteEndElement();

            WriteStartElement("Initializer");
            Visit(value.Initializer);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IGotoStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IGotoStatement");  

            WriteElement("Name", value.Name);
            
            WriteEndElement();
        }
        public void Visit(ILabeledStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("ILabeledStatement");

            WriteElement("Name", value.Name);

            WriteStartElement("Statement");
            Visit(value.Statement);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(ILockStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("ILockStatement");

            WriteStartElement("Body");
            Visit(value.Body);
            WriteEndElement();

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IMemoryCopyStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IMemoryCopyStatement");

            WriteStartElement("Destination"); 
            Manager.ExpressionVisitor.Visit(value.Destination);
            WriteEndElement();

            WriteStartElement("Length"); 
            Manager.ExpressionVisitor.Visit(value.Length);
            WriteEndElement();

            WriteStartElement("Source"); 
            Manager.ExpressionVisitor.Visit(value.Source);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IMemoryInitializeStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IMemoryInitializeStatement");

            WriteStartElement("Length");
            Manager.ExpressionVisitor.Visit(value.Length);
            WriteEndElement();

            WriteStartElement("Offset");
            Manager.ExpressionVisitor.Visit(value.Offset);
            WriteEndElement();

            WriteStartElement("Value");
            Manager.ExpressionVisitor.Visit(value.Value);
            WriteEndElement();
            
            WriteEndElement();
        }
        
        public void Visit(IMethodReturnStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IMethodReturnStatement");

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();
            
            WriteEndElement();
        }
        
        public void Visit(IRemoveEventStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IRemoveEventStatement");

            WriteStartElement("Event");
            Manager.ExpressionVisitor.Visit(value.Event);
            WriteEndElement();

            WriteStartElement("Listener");
            Manager.ExpressionVisitor.Visit(value.Listener);
            WriteEndElement();
            
            WriteEndElement();
        }

        public void Visit(ISwitchStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("ISwitchStatement");

            WriteStartElement("Cases");
            Manager.ILVisitor.Visit(value.Cases);
            WriteEndElement();

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();

            
            WriteEndElement();
        }
        public void Visit(IThrowExceptionStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IThrowExceptionStatement");

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(ITryCatchFinallyStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("ITryCatchFinallyStatement");

            WriteStartElement("CatchClauses");
            Manager.ILVisitor.Visit(value.CatchClauses);
            WriteEndElement();

            WriteStartElement("Fault");
            Visit(value.Fault);
            WriteEndElement();

            WriteStartElement("Finally");
            Visit(value.Finally);
            WriteEndElement();

            WriteStartElement("Try");
            Visit(value.Try);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IUsingStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IUsingStatement");

            WriteStartElement("Body");
            Visit(value.Body);
            WriteEndElement();

            WriteStartElement("Expression");
            Manager.ExpressionVisitor.Visit(value.Expression);
            WriteEndElement();
            
            WriteEndElement();
        }
        public void Visit(IWhileStatement value)
        {
            if (value == null)
                return;

            WriteStartElement("IWhileStatement");

            WriteStartElement("Body");
            Visit(value.Body);
            WriteEndElement();

            WriteStartElement("Condition");
            Manager.ExpressionVisitor.Visit(value.Condition);
            WriteEndElement();
                        
            WriteEndElement();
        }
    }
}
